package com.cardvalue.sys.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.LinearLayout;

import com.alibaba.fastjson.JSON;
import com.cardvalue.sys.R;
import com.cardvalue.sys.adapter.ApplyRecylerAdapter;
import com.cardvalue.sys.base.BaseFragment;
import com.cardvalue.sys.cache.LocalCache;
import com.cardvalue.sys.entity.Apply;
import com.cardvalue.sys.entity.Confirmlist;
import com.cardvalue.sys.entity.PostCreateAuth;
import com.cardvalue.sys.mode.UserService;
import com.cardvalue.sys.tool.LocationUtil;
import com.cardvalue.sys.tool.Utiltools;

import java.util.ArrayList;
import java.util.Date;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.fraudmetrix.android.FMAgent;
import io.realm.Realm;

/**
 * 3：融资保理通知书
 */
public class FinancNoticeFragment extends BaseFragment {
    @Bind(R.id.recyclerView)  RecyclerView mRecyclerView;
    @Bind(R.id.btn_submit) Button btn_submit;//确定按钮
    @Bind(R.id.btn_back) Button btn_back;//返回修改按钮
    @Bind(R.id.ly_bottom) LinearLayout ly_bottom;//控制是否显示

    private ApplyRecylerAdapter  applyRecylerAdapter;
    private UserService userInfo;
    private Realm realm;



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.activity_applysuccess_fragment,container, false);
        ButterKnife.bind(this,view);
        realm = Realm.getDefaultInstance();
        userInfo=new UserService(getActivity());
        //创建一个线性布局管理器,然后设置布局的方向
        LinearLayoutManager  layoutManager= new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayout.VERTICAL);
        //设置布局管理器
        mRecyclerView.setLayoutManager(layoutManager);
        applyRecylerAdapter=new ApplyRecylerAdapter(getActivity());
        //  mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), LinearLayoutManager.VERTICAL));
        mRecyclerView.setAdapter(applyRecylerAdapter);
        initData();

        userInfo.confirmlist("3",new UserService.GetConfirmlistCallback(){
            @Override
            public void success(ArrayList<Confirmlist> confirmlist) {
                Utiltools.printE("confirmlist==333333333333==confirmlist.get(0).getItems()",JSON.toJSONString(confirmlist.get(0).getItems())+"");
                applyRecylerAdapter.update(confirmlist.get(0).getItems());


            }

            @Override
            public void fail(ArrayList<Confirmlist> confirmlist) {

            }

            @Override
            public void cancelDialog() {

            }
        });

        return view;
    }



    @OnClick({R.id.btn_submit,R.id.btn_back}) void onclickButton(View view){
        switch (view.getId()){
            case R.id.btn_back://返回修改
                Apply apply=new Apply();
                apply.setIsSubmitApplication("0");
                userInfo.GetUpdateApplication(apply,new UserService.GetApplyCallback(){
                    @Override
                    public void success(Apply apply) {
                        //成功的话，就重新加载数据
                    }

                    @Override
                    public void cancelDialog() {

                    }

                    @Override
                    public void fail(String msg) {

                    }
                });



                //  startActivity(new Intent(getActivity(), CreditReportActivity.class));
                break;
            case R.id.btn_submit://确认提交
                String blackBox = FMAgent.onEvent();
                Map<String, String> map= LocationUtil.getMap();
                PostCreateAuth task=new PostCreateAuth();
                task.setTime(new Date().toLocaleString() );
                task.setIpAddress("");//IP地址
                task.setBlackBox(blackBox);
                task.setDeviceNumber(map.get("imel")==null?"":map.get("imel").toString());
                task.setGps(map.get("ongitude")+","+map.get("atitude"));
                WebView webview = new WebView(getActivity());
                webview.layout(0, 0, 0, 0);
                WebSettings settings = webview.getSettings();
                String ua = settings.getUserAgentString();
                task.setAgent(ua);
                task.setType(Integer.parseInt("5"));
                userInfo.getUserCreateAuthService(task, new UserService.GetPostCreateAuthCallback() {
                    @Override
                    public void success(PostCreateAuth task) {

                    }

                    @Override
                    public void fail(PostCreateAuth task) {

                    }

                    @Override
                    public void cancelDialog() {

                    }
                });

              //更新申请
                Apply applySubmit=new Apply();
                applySubmit.setBlackBox(blackBox);
                applySubmit.setType("5");
                applySubmit.setIpAddress("");//ip地址
                userInfo.GetUpdateApplication(applySubmit,new UserService.GetApplyCallback(){
                    @Override
                    public void success(Apply apply) {
                        //成功的话，就重新加载数据
                    }

                    @Override
                    public void cancelDialog() {

                    }

                    @Override
                    public void fail(String msg) {

                    }
                });
                break;
        }

    }

    /**
     *一进来就对页面进行设置
     */
    public void initData(){
        Apply results1 =realm.where(Apply.class).equalTo("creditId",LocalCache.getStringLocalData(getActivity(),"creditId")).findFirst();
        Utiltools.printE("type-----","==="+ JSON.toJSONString(results1));
        if (results1 != null){
            String  isDocumentLocked=results1.getIsDocumentLocked()+"";
            if(isDocumentLocked.equals("0")){//可见
                ly_bottom.setVisibility(View.VISIBLE);
            }else{
                ly_bottom.setVisibility(View.GONE);
            }
        }


    }
}
